   <!DOCTYPE html>
<html>
<head>
<head />
<div th:include="base-libs :: base-libs" />
<title>新建医联体</title>
<style type="text/css">
.topBtn{
	float: right;
	margin-right:10px;
	margin-top:5px
}
.laydate-icon{
	border:1px solid #B6B6B6
}
</style>
</head>
<script th:inline="javascript">
	/**发起机构信息*/
	var fqyljgxx = [[${fqyljgxx}]];
	/**签约申请医联体信息*/
	var qysqxx = [[${qysqxx}]];
	/**签约机构信息*/
	var qyjgxx = [[${qyjgxx}]];
</script>
<script type="text/javascript">
	$(function() {
		$(this).registerManagerPage({callback: initData});
		initView();
		//initData(); 
	});

	function initData() {
		/**加载发起机构信息*/
		initFqjg();
		/**加载医联体信息*/
		initQysqxx();
		/**加载签约机构信息*/
		initQyjgxx();
		$("#managerForm").attr("action", "save");
		
		//在选择协议页面返回时加载缓存数据
		initCacheData();
	}

	function initView() {
		$("#method").val("add");
		fqyljgxx[0]["KSDM"] = "";
		fqyljgxx[0]["KSMC"] = "";
		$("#ksTr").find(".ksxx").hide();
		$("#ksTr").find(".yydz").attr("colspan","5");
		$("#ksTr").find(".yydz input").css("width","97.5%");
		/**加载值域*/
		//$(".cacheTable").CacheForSelect();
		var start = {elem: "#QYSJ",choose: function(datas){getDate(datas);}};
		laydate(start);
		$("#QYYXQ").trigger("change");
		/**根据医联体类型改变option*/
		$("#YLTLX").on("change", function(){
			if($(this).val() == "03"){
				$("#ksTr").find(".ksxx").show();
				$("#ksTr").find(".yydz").removeAttr("colspan");
				$("#ksTr").find(".yydz input").css("width","90%");
				$("#KSMC").addClass("form-control");
				$("#jgxxTable").datagrid(getOption($(this).val()));
			}else{
				$("#ksTr").find(".ksxx").hide();
				$("#ksTr").find(".yydz").attr("colspan","5");
				$("#ksTr").find(".yydz input").css("width","97.5%");
				$("#KSMC").removeClass("form-control").val("");
				$("#jgxxTable").datagrid(getOption());
			}
			$("#jgxxTable").datagrid("loadData",{total: 0, rows: []});
		});
		
		$("#jgxxTable").datagrid(getOption());
		
		/**判断有效期是否选择*/
		$("#QYYXQ").on("change", function(){
			var data = $("#QYSJ").val();
			if(data) getDate(data);
		});
		
		/**选择机构*/
		$("#xzYljg").on("click", function(){
			  //页面层
			  var data = $("#jgxxTable").datagrid("getData");
			  qyjgxx = data["rows"];
			  var cahce = {"jgxx": qyjgxx};
			  cahce["ZDYLJGID"] = $("#ZDYLJGID").val();
			  if($("#YLTLX").val() == "03"){
				  cahce["url"] = "queryYljgAndKs.service";
			  }
			  StoreCache.setCache("QYSQ_YLJG",cahce);
			  parent.layer.open({
				  type: 2,
				  area: ["80%", "470px"], //宽高
				  scrollbar: false,
				  resize: false,
				  move: false,
				  title:"医疗机构信息列表",
				  content: contextRoot + "qysq/yljg.html",
				  end: function () {
					var info = StoreCache.removeCache("QYSQ_XZYLJG");
					if(info){
						qyjgxx = info;
						initJgxx();
	              		//$("#jgxxTable").datagrid("loadData", {"total": info.length, "rows": info});
					}
              }
			});
		});
		
		/**选择科室*/
		$("#xzKs").on("click", function(){
			//页面层
			var cache = {};
			cache["PTJGID"] = $("#PTJGID").val();
			cache["KSDM"] = $("#KSDM").val();
			StoreCache.setCache("QYSQ_KSXX",cache);
			parent.layer.open({
				type: 2,
		  		area: ["60%", "500px"], //宽高
		  		scrollbar: false,
		  		resize: false,
				move: false,
				title:"科室列表",
				content: contextRoot + "qysq/kslb.html",
				end: function () {
					var info = StoreCache.removeCache("QYSQ_KSXX");
					if(info){
						fqyljgxx[0]["KSDM"] = info["KSDM"];
						fqyljgxx[0]["KSMC"] = info["KSMC"];
						$("#KSDM").val(info["KSDM"]);
						$("#KSMC").val(info["KSMC"]);
						initData();
					}
				}
			});
		});
		
		
		/**提交按钮*/
		$("#btnXzxy").on("click", function(){
			if($(".form-control").QuickValidate() && validateJg()){
				parent.layer.open({
					  type: 2,
					  area: ["50%", "50%"],
					  content: contextRoot + 'qysq/managerXzxy.html',
					  scrollbar: false,
					  resize: false,
					  title:'选择协议',
					  end: function(){
						  var XYMBID = StoreCache.removeCache("XYMBID"); //协议模板id
						  if(!StringUtils.isEmpty(XYMBID)){
							  var params = {};
							  params["QYZT"] = "02";
							  params.XYID = XYMBID;
							  
							  if(qysqxx){
								  params.YLTID = qysqxx.YLTID;
							  }
								  
							  saveToCache(params);
							  $.QuickUrlUtils.redirectUrl("managerBjxy.html");
							  //submitForm(params);
						  }
					  }
				});
			}
		});
		
		/**暂存按钮*/
		$("#btnZc").on("click", function(){
			if($(".form-control").QuickValidate() && validateJg()){
				var params = {};
				params["QYZT"] = "01";
				submitForm(params);
			}
		});
		
	}

	/** 将数据保存到cache */
	function saveToCache(params){
		//医联体基本信息
		getContainerDataByClassName('yltJbxx', 'dataField', params);
		getParams(params);
		StoreCache.setCache('yltJbxx',params);
		
		//医联体机构信息
		var yltJgxx = $("#jgxxTable").datagrid("getData");
		StoreCache.setCache("yltJgxx",yltJgxx);
	}
	
	/** 加载cache数据  */
	function initCacheData(){
		var yltJbxx = StoreCache.removeCache("yltJbxx");
		if(!$.isEmptyObject(yltJbxx)){
			$(".yltxx").find(".dataField").each(function(){
				$(this).val(yltJbxx[$(this).attr("id")]);
			});
		}
		
    	var yltJgxx = StoreCache.removeCache("yltJgxx");
    	if(yltJgxx){
			qyjgxx = yltJgxx.rows;
			initJgxx();
		}
	}
	
	/**提交表单*/
	function submitForm(params){
		getParams(params);
		
		var tsnr = "";
		if(params["QYZT"] == "01"){
			tsnr="医联体暂存成功";
		}else if(params["QYZT"] == "02"){
			tsnr="医联体新建成功！";
		}
		$.QuickRemote.AjaxFiledFormSubmit("managerForm", function(result) { 
			var info = result["info"];
			if (result["state"]) {
				$.QuickAlert.alertNormal(tsnr,{callback: function(){
					$.QuickUrlUtils.redirectUrl("index.html");
				}});
			} else {
				$.QuickAlert.alertFail({
					content : info["faultInfo"]
				});
			}
		}, params);
	}
	
	/**返回按钮*/
	function btnBackHandler() {
		$.QuickUrlUtils.redirectUrl("index.html");
	}
	
	/** 获取保存数据 */
	function getParams(params){
		/**获取机构IDs*/
		var info = $("#jgxxTable").datagrid("getData");
		var rows = info["rows"];
		var yljgids = "";
		if($("#YLTLX").val() == "03"){
			var ksdms = "";
			for(var i in rows){
				yljgids += rows[i]["PTJGID"] + ",";
				ksdms += rows[i]["PTJGID"] + ":" + rows[i]["KSDM"] + "/";
			}
			//ksdms += $("#PTJGID").val() + ":" + $("#KSDM").val() + "/";
			params["KSDMS"] = ksdms.substr(0, ksdms.length-1);
		}else{
			for(var i in rows){
				yljgids += rows[i]["PTJGID"] + ",";
			}
		}
		yljgids = yljgids.substr(0, yljgids.length -1);
		params["YLJGIDS"] = yljgids;
		//params["FQYLJGID"] = $("#PTJGID").val();
		params["ZDYLJGID"] = $("#ZDYLJGID").val();
		params["BZSM"] = $("#BZSM").val();
		params["method"] = $("#method").val();
	}
	
	/**加载发起机构信息*/
	function initFqjg(){
		var jgxx = fqyljgxx[0];
		$(".fqjg").find("input").each(function(){
			$(this).val(jgxx[$(this).attr("id")]);
		});
		$("#ZDYLJGID").val(jgxx["PTJGID"]);
		$("#ZDYLJGMC").val(jgxx["YLJGMC"]);
		initJgxx();
	}
	
	/**移除医疗机构*/
	function remove(index){
		$("#jgxxTable").datagrid("deleteRow",index);
		var data = $("#jgxxTable").datagrid("getData");
		/**清除所有数据*/
		$("#jgxxTable").datagrid("loadData", { total: 0, rows: [] }); 
		/**重新加载数据*/
		$("#jgxxTable").datagrid("loadData",data);
		data = $("#jgxxTable").datagrid("getData");
	}
	
	/**校验医疗机构*/
	function validateJg(){
		var info = $("#jgxxTable").datagrid("getData");
		var flag = false;
		if(info["total"] == "1"){
			$.QuickAlert.alertFail({
				content : "请添加医疗机构！",
			});
		}else{
			flag = true;
		}
		return flag 
	}
	
	/**加载医联体信息*/
	function initQysqxx(){
		if(qysqxx){
 			$("#method").val("update");
			var info;
			$(qyjgxx).each(function(index,item){
				if(item["JGLX"]=="3"){
					info = item;
				}
			});
			info = $.extend({}, qysqxx, info);
			$(".yltxx").find(".dataField").each(function(){
				$(this).val(info[$(this).attr("id")]);
			});
			$("#BZSM").val(qysqxx["BZSM"]);
			$("#YLTID").val(qysqxx["YLTID"]);
			if($("#YLTLX").val()=="03"){
				$("#ksTr").find(".ksxx").show();
				$("#ksTr").find(".yydz").removeAttr("colspan");
				$("#ksTr").find(".yydz input").css("width","90%");
				$("#KSMC").attr("quick-config","label:'科室名称', validateProxy:'notNull'");
			}else{
				$("#ksTr").find(".ksxx").hide();
				$("#ksTr").find(".yydz").attr("colspan","5");
				$("#ksTr").find(".yydz input").css("width","97.5%");
				$("#KSMC").val("");
				$("#KSMC").removeAttr("quick-config")
			}
		}
	}
	
	/**加载签约机构信息*/
	function initQyjgxx(){
		if(qyjgxx){
			if(qysqxx["YLTLX"] == "03"){
				$("#jgxxTable").datagrid(getOption(qysqxx["YLTLX"]));
				for ( var i in qyjgxx) {
					if(qyjgxx[i]["PTJGID"]==qysqxx["ZDYLJGID"]){
						$("#KSDM").val(qyjgxx[i]["KSDM"]);
						$("#KSMC").val(qyjgxx[i]["KSMC"]);
					}
				}
			}
			initJgxx();
		}
	}
	
	/**获取option*/
	function getOption(lx){
		var opt;
		if(lx == "03"){
			opt= {
					idField : "PTJGID",
					uniqueId: "PTJGID",
					striped : true,
					border:false,
					clickToSelect: true,
					fit: true,
					fitColumns: true,
					singleSelect: false,
					rownumbers: true,
					columns : [[
						{
							field : "YLJGMC",
							width: 160,
							align: "center",
							title : "机构名称"
						},
						{
							field : "XZQHMC",
							width: 100,
							align: "center",
							title : "所属区域"
						},
						{
							field : "WSJGLBMC",
							width: 120,
							align: "center",
							title : "机构类型"
						},
						{
							field : "KSMC",
							width: 200,
							align: "center",
							title : "科室",
							formatter : function(value, row, index){
								return "<span title='"+value+"'>"+value+"</span>";
							}
						},
						{
							field : "YYDJMC",
							width: 100,
							align: "center",
							title : "医院等级"
						},
						{
							field : "JGFLGLMC",
							width: 120,
							align: "center",
							title : "经营性质"
						},
						{
							field : "cz",
							width: 100,
							align: "center",
							title : "操作",
							formatter : function(value, row, index){
								if(row["PTJGID"] != $("#ZDYLJGID").val()){
									value = $("#tableToolBar").html()
									return value.replace(/#index#/g,index);
								}else{
									$("#KSDM").val(row["KSDM"]);
									$("#KSMC").val(row["KSMC"]);
								}
							}
						}
					]],onLoadSuccess: function(data){
						/**在签约申请下不可操作*/
						/* if($("#method").val() == "qysq"){
							 $("#jgxxTable").datagrid("hideColumn", "cz"); // 设置隐藏列   
						} */
						/**假如没有数据，则默认加入当前机构*/
						if(data["total"]=="0"){
							initJgxx();
						}
						$("#jgxxLayout").layout("resize");
					}
				};
		}
		else{
			opt= {
					idField : "PTJGID",
					uniqueId: "PTJGID",
					striped : true,
					border:false,
					clickToSelect: true,
					fit: true,
					fitColumns: true,
					singleSelect: false,
					rownumbers: true,
					columns : [[
						{
							field : "YLJGMC",
							width: 250,
							align: "center",
							title : "机构名称"
						},
						{
							field : "XZQHMC",
							width: 150,
							align: "center",
							title : "所属区域"
						},
						{
							field : "WSJGLBMC",
							width: 150,
							align: "center",
							title : "机构类型"
						},
						{
							field : "YYDJMC",
							width: 150,
							align: "center",
							title : "医院等级"
						},
						{
							field : "JGFLGLMC",
							width: 150,
							align: "center",
							title : "经营性质"
						},
						{
							field : "cz",
							width: 100,
							align: "center",
							title : "操作",
							formatter : function(value, row, index){
								if(row["PTJGID"] != $("#ZDYLJGID").val()){
									value = $("#tableToolBar").html()
									return value.replace(/#index#/g,index);
								}else{
									return "主导机构";
								}
							}
						}
					]],onLoadSuccess: function(data){
						/**签约申请不可操作*/
						/* if($("#method").val() == "qysq"){
							 $("#jgxxTable").datagrid("hideColumn", "cz"); // 设置隐藏列   
						} */
						/**假如没有数据，则默认加入当前机构*/
						if(data["total"]=="0"){
							initJgxx();
						}
						$("#jgxxLayout").layout("resize");
					}
				};
		}
		return opt;
	}
	

	
	/**生成到期时间*/
	function getDate(datas){
		datas = datas.split("-");
		var lx = $("#QYYXQ").val();
		if(StringUtils.isEmpty(lx)){
			//用户可能先选择时间啊啊啊
			return;
		}else if(lx == "01"){
    		datas = new Date(datas[0], parseInt(datas[1]) -1 + 1, datas[2]); 
    	}
    	else if(lx == "02"){
    		datas = new Date(datas[0], parseInt(datas[1]) -1 + 3, datas[2]); 
    	}
		else if(lx == "03"){
			datas = new Date(datas[0], parseInt(datas[1]) -1 + 6, datas[2]); 
    	}
		else if(lx == "04"){
			datas = new Date(parseInt(datas[0]) + 1, datas[1], datas[2]); 
		}
    	datas = datas.getFullYear()+"-"+(parseInt(datas.getMonth()) +1) +"-"+datas.getDate();
    	$("#DQSJ").val(datas);
	}
	
	/**初始化选择机构table*/
	function initJgxx(){
		var ary = [];
		if(qyjgxx){
			if(isExit()){
				ary = qyjgxx.concat(fqyljgxx);
			}else{
				ary = qyjgxx;
			}
		}else{
			ary = fqyljgxx;
		}
		var length = 37*(ary.length);
		$("#maxDiv").css("height",length+670+"px");
		$("#maxLayout").layout("resize");
		$("#jgxxBody").css("height", length + 43+"px");
		$("#jgxxTable").datagrid("loadData",{"total": ary.length, "rows": ary});
	}
	
	/**判断发起机构是否存在机构列表中*/
	function isExit(){
		var flag = true;
		$(qyjgxx).each(function(index, item){
			if(item["PTJGID"] == fqyljgxx[0]["PTJGID"]){
				flag = false;
			}
		});
		return flag;
	}
</script>

<body style="overflow-y: auto;">
<div id="maxDiv" style="height: 880px;">
	<div id="maxLayout" class="easyui-layout" fit="true">
		<div region="north" style="border: none; overflow: hidden;">
			<h4 class="h4-title" style="font-size: 18px;text-align:center;">
				新建医联体
			</h4>
			<div class="form-table-box" >
				<input id="method" style="display: none;">
				<form id="managerForm" class="form-horizontal" >
					<h4 class="h4-title" style="font-size: 13px;">
						签约申请机构
					</h4>
					<table class="form-table fqjg">
						<tr>
							<td class="td-text">机构名称：</td>
							<td class="td-content">
								<input id="PTJGID" style="display: none;" class="form-control dataField" type="text"
									readonly="readonly">
								<input id="YLJGMC" class="form-control dataField" type="text"
									readonly="readonly">
							</td>
							<td class="td-text">机构编码：</td>
							<td class="td-content">
								<input id="YLJGDM" class="form-control dataField" type="text"
									readonly="readonly">
							</td>
							<td class="td-text">所属区域：</td>
							<td class="td-content">
								<input id="XZQHMC" class="form-control dataField" type="text"
									readonly="readonly">
							</td>
						</tr>
						
						<tr>
							<td class="td-text">机构类型：</td>
							<td class="td-content">
								<input id="WSJGLBMC" class="form-control dataField" type="text"
									readonly="readonly">
							</td>
							<td class="td-text">经营性质：</td>
							<td class="td-content">
								<input id="JGFLGLMC" class="form-control dataField" type="text"
									readonly="readonly">
							</td>
							<td class="td-text">医院等级：</td>
							<td class="td-content">
								<input id="YYDJMC" class="form-control dataField" type="text"
									readonly="readonly">
							</td>
						</tr>
						<tr id="ksTr">
							<td class="td-text">医院地址：</td>
							<td class="td-content yydz">
								<input id="DWDZ" class="form-control dataField" type="text" 
									readonly="readonly">
							</td>
							<td class="td-text ksxx">科室名称：</td>
							<td class="td-content ksxx" >
								<input id="KSMC" class="dataField" type="text"
								placeholder="科室名称" readonly="readonly" quick-config="label:'科室名称', validateProxy:'notNull'">
								<input id="KSDM" style="display:none;">
							</td>
							<td class="td-text ksxx">
								<button class="btn" type="button"  id="xzKs">选择科室</button>
							</td>
						</tr>
					</table>
					<h4 class="h4-title" style="font-size: 13px;">
						医联体信息
					</h4>
					<table class="form-table yltxx" id="yltJbxx">
						<tr>
							<td class="td-text">
								<label>医联体名称：</label>
							</td>
							<td class="td-content">
								<input id="YLTID" name="YLTID" style="display: none;"/>
								<input id="YLTMC" name="YLTMC" class="form-control dataField yltxx" type="text" 
									quick-config="label:'医联体名称', validateProxy:'notNull length', length: '50'" 
									placeholder="医联体名称">
							</td>
							<td class="td-text">
								<label>医联体类型：</label>
							</td>
							<td class="td-content">
								<select id="YLTLX" name="YLTLX" class="form-control dataField yltxx" style="width:90%;"
								cacheTable="TB_ZY_YLTLX"	quick-config="label:'医联体类型', validateProxy:'notNull'">
								</select>
							</td>
							<td class="td-text">
								<label>主导医疗机构：</label>
							</td>
							<td class="td-content">
								<!-- <select id="ZDYLJGID" class="form-control dataField" style="width:90%;">
								</select> -->
								<input id="ZDYLJGMC" readonly="readonly" class="yltxx">
								<input id="ZDYLJGID" style="display:none;">
							</td>
						</tr>
						<tr>
							<td class="td-text">
								<label>签约有效期：</label>
							</td>
							<td class="td-content">
								<select id="QYYXQ" name="QYYXQ" class="form-control dataField cacheTable yltxx" style="width:90%;"
								cacheTable="TB_ZY_YXQ" quick-config="label:'签约有效期', validateProxy:'notNull'">
								</select>
							</td>
							<td class="td-text">
								<label>签约时间：</label>
							</td>
							<td class="td-content">
								<input id="QYSJ" name="QYSJ" style="height:30px; width: 80%;" readonly="readonly"
								class="form-control input-text laydate-icon dataField yltxx" quick-config="label:'签约时间', validateProxy:'notNull'" />
							
							</td>
							<td class="td-text">至</td>
							<td class="td-content">
								<input id="DQSJ" name="DQSJ" readonly="readonly"
								class="form-control dataField yltxx" quick-config="label:'到期时间', validateProxy:'notNull'" /><!-- onclick="laydate(end)" -->
							</td> 
						</tr>
					</table>
				</form>
			</div>
		</div>
		<div region="center" style="border: none;overflow: hidden;">
			<!-- <div  id="jgxxBody" style="height:120px">
				<div  id="jgxxLayout" class="easyui-layout" fit="true">
					<div region="center" style="border: none">
						<h4 class="h4-title" style="font-size: 13px;">
						添加机构
							<div style="float:right;">
								<button class="btn yltxx" type="button" style="width:120px" id="xzYljg">选择机构</button>
							</div>
						</h4>
						<table id="jgxxTable">
									</table>
					</div>
					
				</div>
			</div> -->
			<h4 class="h4-title" style="font-size: 13px;">
				添加机构
				<div style="float:right;">
					<button class="btn yltxx" type="button" style="width:120px" id="xzYljg">选择机构</button>
				</div>
			</h4>
			<div  id="jgxxBody" style="height:120px;width:96.5%;margin-left:2.5%;">
				<div  id="jgxxLayout" class="easyui-layout" fit="true">
					<div region="center" style="border: none;">
						<table id="jgxxTable" >
									</table>
					</div>
				</div>
			</div>
			
			
			<div style="height: 320px;">
				<div class="easyui-layout" fit="true" >
					<div region="center" style="border: none">
						<div class="form-table-box" >
							<table class="form-table">
								<tr>
									<td class="td-text">备注说明：</td>
									<td class="td-content" colspan="5">
										<input id="BZSM" name="BZSM" class="form-control yltxx" style="width:97.5%" 
										quick-config="label:'备注说明', validateProxy:'length', length: '200'" />
									</td>
								</tr>
							</table>
							<!-- <div class="form-table-box" >
								<h4 class="h4-title" style="font-size: 13px;">
									服务协议信息
									<div style="float:right;">
										<button class="btn yltxx" type="button" style="width:120px" id="xzXy">选择协议</button>
									</div>
								</h4>
								<table class="form-table">
									<tr>
										<td class="td-content">
											<input class="easyui-textbox" readonly="readonly" data-options="multiline:true" style="width:100%;height:100px">
										</td>
									</tr>
								</table>
							</div> -->
							<div style="text-align: center;padding-bottom:10px;padding-top:5px;">
								<button class="btn btn-primary" type="button" id="btnXzxy" >选择协议</button>
								&nbsp;
								<button class="btn btn-default" type="button" id="btnZc">暂存</button>
								&nbsp;
								<button class="btn btn-default" type="button" onclick="btnBackHandler()">返回</button>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</body>
<script type="text/html" id="tableToolBar">
<a class="tableToolBarBtn" href="javascript:remove(#index#)" style="" title="移除" >移除</a>
</script>
</html>


